package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-03
 */
@RestController
@RequestMapping("/eduservice/subject")
@Api(tags = "课程分类管理")
//@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService eduSubjectService;

    @ApiOperation("添加课程分类")
    @PostMapping("addSubject")
    public R addSubject(@RequestBody EduSubject eduSubject) {
        boolean save = eduSubjectService.save(eduSubject);
        if (save) {
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("修改课程分类")
    @PutMapping("updateSubject")
    public R updateSubject(@RequestBody EduSubject eduSubject) {
        boolean update = eduSubjectService.updateById(eduSubject);
        if (update) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("查看全部课程分类")
    @GetMapping("getAllSubject")
    public R getAllSubject() {
        List<EduSubject> list = eduSubjectService.list(null);
        return R.ok().data("list",list);
    }

    @ApiOperation("查看一级课程分类")
    @GetMapping("getOneSubject")
    public R getOneSubject() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> list = eduSubjectService.list(wrapper);
        return R.ok().data("list",list);
    }

    @ApiOperation("根据id查询课程分类")
    @GetMapping("getSubject/{id}")
    public R getSubject(@PathVariable String id) {
        EduSubject subject = eduSubjectService.getById(id);
        return R.ok().data("subject",subject);
    }
    
    @ApiOperation("删除课程分类")
    @DeleteMapping("deleteSubject/{id}")
    public R deleteSubject(@PathVariable String id) {
        eduSubjectService.deleteById(id);
        return R.ok();
    }



}

